package service.tajaApp.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.stereotype.Repository;

import service.domain.BoardVO;
import service.domain.EventVO;
import service.domain.GameVO;
import service.domain.RankVO;
import service.domain.UserVO;
import service.tajaApp.TajaAppDAO;

import com.ibatis.sqlmap.client.SqlMapClient;

@Repository("tajaAppDAO")
public class TajaAppDAOImpl implements TajaAppDAO {

	@Inject
	@Named("sqlMapClient")
	private SqlMapClient sqlMapClient;
	
	public void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClient = sqlMapClient;
	}
		
	public TajaAppDAOImpl() {
	}
	
	public List getRankList() throws Exception {
		return (ArrayList<RankVO>)sqlMapClient.queryForList("TajaApp.getRankList");		
	}
	
	public List getEventList() throws Exception {
		return (ArrayList<EventVO>)sqlMapClient.queryForList("TajaApp.getEventList");		
	}
	
	public List getTodayEvent() throws Exception {
		return (ArrayList<EventVO>)sqlMapClient.queryForList("TajaApp.getTodayEvent");		
	}
	
	public GameVO getGameInfo(String userId) throws Exception {
		return (GameVO)sqlMapClient.queryForObject("TajaApp.getGameInfo", userId);
	}

	public boolean addGameInfo(String userId) throws Exception {
		Map<String, String> gameInfo = new HashMap<String, String>();
		gameInfo.put("userId", userId);
		gameInfo.put("eventId", (String)sqlMapClient.queryForObject("TajaApp.getTodayEvent"));
		
		if(sqlMapClient.insert("TajaApp.addGameInfo", gameInfo) == null) {
			return true;
		}
		return false;
	}

	public GameVO updateGameInfo(String userId, String gameName, long gameTime) throws Exception {
		GameVO gameVO = (GameVO)sqlMapClient.queryForObject("TajaApp.getGameInfo", userId);
		Map gameInfo = new HashMap();
		gameInfo.put("userId", userId);
		gameInfo.put("eventId", (String)sqlMapClient.queryForObject("TajaApp.getTodayEvent"));
		gameInfo.put("gameLife", gameVO.getGameLife()-1);		
		if(gameName.equals("touchGame") && (gameVO.getTouchTime()==0 || gameVO.getTouchTime()>gameTime)) {
			gameInfo.put(gameName, gameTime);			
		} else if(gameName.equals("moaGame") && (gameVO.getMoaTime()==0 || gameVO.getMoaTime()>gameTime)) {
			gameInfo.put(gameName, gameTime);			
		} else if(gameName.equals("crashGame") && (gameVO.getCrashTime()==0 || gameVO.getCrashTime()>gameTime)) {
			gameInfo.put(gameName, gameTime);			
		}		
		sqlMapClient.update("TajaApp.updateGameInfo", gameInfo);
		return gameVO;
	}
	
	public UserVO getUserInfo(String userId) throws Exception {
		return (UserVO)sqlMapClient.queryForObject("TajaApp.getUserInfo", userId);
	}

	public boolean addUserInfo(UserVO userVO) throws Exception {
		if(sqlMapClient.insert("TajaApp.addUserInfo", userVO) == null) {
			return true;
		}
		return false;
	}
	
	public List getBoardList() throws Exception {
		return (ArrayList<BoardVO>)sqlMapClient.queryForList("TajaApp.getBoardList");
	}
		
} // end of class